<template>
  <div class="app-container">

    <el-row >
      <el-col :span="24">
        <span>本月正常出勤: {{ attendanceStatistics.normalDays }}天</span>
        <span style="margin-left: 20px;">迟到: {{ attendanceStatistics.lateCount }}次</span>
        <span style="margin-left: 20px;">早退: {{ attendanceStatistics.earlyCount }}次</span>
        <span style="margin-left: 20px;">矿工: {{ attendanceStatistics.absentCount }}次</span>
      </el-col>
    </el-row>

    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="开始日期" prop="startDate">
        <el-date-picker
          v-model="queryParams.startDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择开始日期"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="结束日期" prop="endDate">
        <el-date-picker
          v-model="queryParams.endDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择结束日期"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="打卡状态" clearable>
          <el-option
            v-for="dict in dict.type.am_ait_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        <el-button size="mini" @click="morningDak">上班打卡</el-button>
        <el-button size="mini" @click="afternoonDak">下班打卡</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="aitList">
      <el-table-column label="id" align="center" prop="id" />
      <el-table-column label="姓名" align="center" prop="nickName" />
      <el-table-column label="上午打卡时间" align="center" prop="morningPunch">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.morningPunch, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="下午打卡时间" align="center" prop="afternoonPunch">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.afternoonPunch, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="打卡状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.am_ait_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>

</template>

<script>
import {listAit, afternoonDak, morningDak, getAttendanceStatistics} from "@/api/am/ait";

export default {
  name: "Ait",
  dicts: ['am_ait_status'],
  data() {
    return {
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 岗位表格数据
      aitList: [],
      // 遮罩层
      loading: true,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        startDate: undefined,
        endDate: undefined,
        nickName: undefined,
        status: undefined
      },
      // 列信息
      columns: {
        id: { label: 'id', visible: true },
        nickName: { label: '姓名', visible: true },
        morningPunch: { label: '上午打卡时间', visible: true },
        afternoonPunch: { label: '下午打卡时间', visible: true },
        status: { label: '状态', visible: true }
      },
      attendanceStatistics: {
        normalDays: 0,
        lateCount: 0,
        earlyCount: 0,
        absentCount: 0
      }

    }
  },
  created() {
    this.getList()
    this.loadAttendanceStatistics()
  },
  methods: {
    /** 查询岗位列表 */
    getList() {
      this.loading = true
      listAit(this.queryParams).then(response => {
        this.aitList = response.rows
        this.total = response.total
        this.loading = false
      })
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    morningDak(){
      morningDak().then(response => {
        this.$message({
          message: '上班打卡成功',
          type: 'success'
        })
        this.getList()
      })
    },
    afternoonDak(){
      afternoonDak().then(response => {
        this.$message({
          message: '下班打卡成功',
          type: 'success'
        })
        this.getList()
      })
    },// 加载本月考勤统计信息
    loadAttendanceStatistics() {
      getAttendanceStatistics().then(response => {
        if (response.code === 200) {
          const data = response.data || {}
          this.attendanceStatistics = {
            normalDays: data.normalCount || 0,  // 注意这里修改为normalCount
            lateCount: data.lateCount || 0,     // 这个字段名一致
            earlyCount: data.earlyLeaveCount || 0,  // 注意这里修改为earlyLeaveCount
            absentCount: data.actualAbsentCount || 0  // 注意这里修改为actualAbsentCount
          }
        }
      })
    }
  }
}
</script>

<style scoped lang="scss">
.el-row {
  margin-bottom: 20px;
  &:last-child {
    margin-bottom: 0;
  }
}
.el-col {
  border-radius: 4px;
}
</style>
